package com.gem.shop.web;

import com.gem.shop.entity.Customer;
import com.gem.shop.service.CustomerService;
import com.gem.shop.util.BeanFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author zhang
 * @date 2020/9/18 15:27
 */
@WebServlet("/customer/*")
public class CustomerServlet extends HttpServlet {
    CustomerService customerService = (CustomerService) BeanFactory.getBean("customerService");
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        PrintWriter writer = resp.getWriter();


        //跳转注册页面
        if ("/toRegister".equals(pathInfo)){
            req.getRequestDispatcher("/WEB-INF/pages/register.jsp").forward(req,resp);
        }

        //注册功能
        if ("/register".equals(pathInfo)){
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            customerService.register(username,password);
            writer.print("1");
        }

        //登录功能
        if ("/login".equals(pathInfo)){
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            HttpSession session = req.getSession();

            String username = req.getParameter("username");
            String password = req.getParameter("password");
            Customer customer = customerService.login(username,password);
            if (customer == null){
                //登录失败
                writer.print("0");
            }else {
                //登录成功
                String remeberMe = req.getParameter("remeberMe");
                if ("1".equals(remeberMe)){
                    //使用记住账号密码功能
                    Cookie cookie = new Cookie("customer",username + ":" + password);
                    //设置最大保存时间
                    cookie.setMaxAge(7 * 24 * 60 * 60);
                    resp.addCookie(cookie);
                }else {
                    //不使用记住账号密码功能
                    Cookie[] cookies = req.getCookies();
                    Cookie customerCookie = getCookie(cookies, "customer");
                    if (customerCookie != null){
                        customerCookie.setMaxAge(0);
                        resp.addCookie(customerCookie);
                    }
                }

                //利用session保存顾客信息
                session.setAttribute("customer",customer);
                writer.print("1");
            }
        }

        //退出登录
        if ("/logout".equals(pathInfo)){
            //销毁session
            HttpSession session = req.getSession();
            session.removeAttribute("customer");
            resp.sendRedirect(req.getContextPath() + "/index.jsp");
        }

        //检查用户名
        if ("/checkUsername".equals(pathInfo)){
            PrintWriter printWriter = resp.getWriter();
            String username = req.getParameter("username");
            Boolean bool = customerService.checkUsername(username);
            if (bool){
                //用户名可用
                printWriter.print("1");
            }else {
                printWriter.print("0");
            }
        }

        //跳转到更新信息页面
        if ("/toUpdateCustomer".equals(pathInfo)){
            String idStr = req.getParameter("id");
            int id = Integer.parseInt(idStr);
            Customer customer = customerService.findCustomerById(id);
            req.setAttribute("customer",customer);
            req.getRequestDispatcher("/WEB-INF/pages/updateCustomer.jsp").forward(req,resp);
        }

        //更新信息
        if("/updateCustomer".equals(pathInfo)){
            System.out.println("进入修改方法");
            String idStr = req.getParameter("id");
            String name = req.getParameter("name");
            String phone = req.getParameter("phone");
            String address = req.getParameter("address");
            String postCodeStr = req.getParameter("postCode");
            int postCode = Integer.parseInt(postCodeStr);
            int id = Integer.parseInt(idStr);
            Customer customer = customerService.findCustomerById(id);
            customer.setName(name);
            customer.setPhone(phone);
            customer.setAddress(address);
            customer.setPostCode(postCode);
            HttpSession session = req.getSession();

            //将原本session覆盖
            session.setAttribute("customer",customer);
            customerService.eidtCustomer(customer);
            writer.print("1");
        }




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }



    private Cookie getCookie(Cookie[] cookies,String name){
        Cookie cookie = null;
        for (Cookie c : cookies) {
            if (c.getName().equals(name)){
                cookie = c;
                break;
            }
        }
        return cookie;
    }
}
